﻿
@{
    ViewBag.Title = "AdminUserEditView";
    Layout = "~/Views/Shared/_MyLayout.cshtml";
}

<div id="addLayer" style="display:block;">
    <form class="layui-form" id="addCardFrom" style="margin-top:10px;">
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="UserName"  value="@ViewBag.UserName" lay-verify="required" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">电话</label>
                <div class="layui-input-inline">
                    <input type="tel" name="Tel" value="@ViewBag.Tel" lay-verify="required|phone" class="layui-input">
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">部门</label>
            <div class="layui-inline">
                <input type="text" name="Department" value="@ViewBag.Department" lay-verify="required" class="layui-input">
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <label class="layui-form-label"></label>
                    <button type="button" class="layui-btn" id="BtnSave">保存</button>
                </div>
            </div>
        </div>
        <button type="submit" id="mysubmit" class="layui-btn" lay-submit lay-filter="formVerify" style="display:none;">立即提交</button>
    </form>
</div>
<script type="text/javascript">
    var UserID="@ViewBag.UserID.ToString()";
    var table;
    layui.use(['form','table', 'upload', 'layer'], function () {
        var form = layui.form;
        table = layui.table;
        var layer = layui.layer;
        var IsValidate = 0;

        form.on('submit(formVerify)', function (data) {
            //layer.msg(JSON.stringify(data.field));
            IsValidate = 1;
            return false;
        });

        $("#BtnSave").on("click", function () {
            
            $("#mysubmit").click();
            if (IsValidate == 1) {
                var obj = $("#addCardFrom").serializeArray();
                var data = {};
                $.each(obj, function (i, v) {
                    data[v.name] = v.value;
                });
                if (UserID == "") {
                    //表示新增
                    $.ajax({
                        type: "POST",
                        url: "/User/AdminAddUser",
                        async: false,
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(data),
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('新增成功', { icon: 6, time: 1000 });
                                parent.table.reload("demo");
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                                
                                //parent.layui.admin.events.closeThisTabs();
                            } else {
                                alert(msg.result);
                            }
                        }
                    });


                }
                else {
                    //表示编辑
                    data["UserID"] = UserID;
                    $.ajax({
                        type: "POST",
                        url: "/User/AdminUpdateUser",
                        async: false,
                        contentType: "application/json",
                        dataType: "json",
                        data: JSON.stringify(data),
                        success: function (msg) {
                            if (msg.result == "OK") {
                                parent.layer.msg('修改成功', { icon: 6, time: 1000 });
                                parent.table.reload("demo");
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);

                                //parent.layui.admin.events.closeThisTabs();
                            } else {
                                alert(msg.result);
                            }
                        }
                    });


                }
                //alert(JSON.stringify(data));


            }




        });






    });








</script>
